#include "stdio.h"
#include "string.h"

int main(){
    int n=0;
    scanf("%d",&n);
    char arr[10001];
    getchar();
    for (int i = 0; i < n; ++i) {
        int flag=1;
        scanf("%s",arr);
        getchar();
        int count1=0,count2=0,count3=0;
        int len = strlen(arr);
        int flag_p=1,flag_t=1;
        for (int j = 0; j < len; ++j) {
            if(arr[j]!='A'&&arr[j]!='P'&&arr[j]!='T'){
                printf("NO\n");
                flag=0;
                break;
            }
            if(flag_p==0 && arr[j]=='P'){
                printf("NO\n");
                flag=0;
                break;
            }
            if(flag_t==0 && arr[j]=='T'){
                printf("NO\n");
                flag=0;
                break;
            }
            if(flag_p==1 && arr[j]=='T'){
                printf("NO\n");
                flag=0;
                break;
            }
            if (arr[j]=='P'){
                flag_p=0;
            }
            if(arr[j]=='T'){
                flag_t=0;
            }
            if(flag_p){
                if(arr[j]=='A'){
                    count1++;
                }
            }
            if(flag_p==0&&flag_t==1){
                if(arr[j]=='A'){
                    count2++;
                }
            }
            if(flag_p==0&&flag_t==0){
                if(arr[j]=='A'){
                    count3++;
                }
            }
        }
        if (flag){
            if(flag_p!=0 || flag_t!=0){
                printf("NO\n");
            }
            else if(count1*count2==count3 && (count1+count2+count3)!=0){
                printf("YES\n");
            } else{
                printf("NO\n");
            }
        }
    }
    return 0;
}